﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Threading;
//using IWshRuntimeLibrary;



namespace 玄鸟刀剑2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();


            f.ShowDialog();

        }
        /// <summary>
        /// 文本取中间
        /// </summary>
        /// <param name="startChar"></param>
        /// <param name="endChar"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private string Match_Str(string startChar, string endChar, string str)
        {
            try
            {
                Regex reg = new Regex("(?<=" + startChar + ").*?(?=" + endChar + ")", RegexOptions.IgnoreCase);
                Match m = reg.Match(str);
                if (m.Success)
                {
                    return m.ToString();
                }
            }
            catch (System.Exception ex)
            {

            }
            return "";
        }


        private void button2_Click(object sender, EventArgs e)
        {

            //var request = (HttpWebRequest)WebRequest.Create("https://cloud.189.cn/t/UBrqeiJvqAza");   //版本更新
            //var response = (HttpWebResponse)request.GetResponse();
            //var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);


            if (File.Exists("game.zip"))
            {
                File.Delete("game.zip");
            }









            MD5 mD5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.UTF8.GetBytes(textBox2.Text);
            byte[] targetData = mD5.ComputeHash(fromData);
            string byte25tring = null;
            for (int i = 0; i < targetData.Length; i++)
            {
                byte25tring += targetData[i].ToString("x2");
            }


            // var request = (HttpWebRequest)WebRequest.Create("http://110.80.137.159:81/reg.php?u=" + textBox1.Text.Trim() + "&p=" + byte25tring);

            var request = (HttpWebRequest)WebRequest.Create("http://111.67.201.175:81/login.php?u=" + textBox1.Text.Trim() + "&p=" + byte25tring);



            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            // MessageBox.Show(byte25tring);
            //  MessageBox.Show(responseString);


            if (responseString.Length <= 3)
            {

                MessageBox.Show("登陆失败,账号或密码错误");
                return;
            }
            // MessageBox.Show("可以登陆游戏了");


            string[] data = System.Text.RegularExpressions.Regex.Split(responseString, @"[|]");

            // MessageBox.Show(responseString);
            //  MessageBox.Show(data[0]);

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"game\release_mtp\update_cfg.xml"))
            {
                File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"game\release_mtp\update_cfg.xml");
            }

            string srcFileName = AppDomain.CurrentDomain.BaseDirectory + @"download\xys.dll";
            string destFileName = AppDomain.CurrentDomain.BaseDirectory + @"game\release_mtp\update_cfg.xml";
            if (File.Exists(srcFileName))
            {
                File.Move(srcFileName, destFileName);
            }




            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"game\release_mtp\patcher.exe";//需要启动的程序名      
                                                                                                           //a启动参数 

            // p.StartInfo.Arguments = " -u " + responseString + " -p 1111 -z 1"; //启动参数  
            p.StartInfo.Arguments = " -u " + data[0] + " -p 1111 -z 1"; //启动参数  

            p.Start();//启动    

            p.WaitForExit();

            File.Move(destFileName, srcFileName);



            System.Environment.Exit(0);


            return;





        }
        /// <summary>
        /// 多线程
        /// </summary>
        void printnumberswithdelay()
        {

            // string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            // CreateShortcut(desktop + @"\玄鸟刀剑2.lnk", "");

            // var request = (HttpWebRequest)WebRequest.Create("https://dj-1300382743.cos.ap-chengdu.myqcloud.com/url.txt");//更新game
            // var response = (HttpWebResponse)request.GetResponse();
            // var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            // request = (HttpWebRequest)WebRequest.Create(responseString);
            // response = (HttpWebResponse)request.GetResponse();
            // responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //var responseString1 = Match_Str("分享时间:", "下载", responseString);
            // Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


            // if (responseString1 != ConfigurationManager.AppSettings["ver"])
            // {
            //      MessageBox.Show("程序需要更新,请点确认");

            //     config.AppSettings.Settings["ver"].Value = responseString1;
            //     config.Save(ConfigurationSaveMode.Modified);
            //     ConfigurationManager.RefreshSection("appSettings");

            //     string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);
            //     System.Diagnostics.Process p = new System.Diagnostics.Process();
            //     p.StartInfo.CreateNoWindow = true;
            //     p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"djupdate.exe";//需要启动的程序名         //a启动参数 

            //     p.StartInfo.Arguments = url; //启动参数  
            //     MessageBox.Show(url);


            //     p.Start();//启动    

            //     p.WaitForExit();              

            //     System.Environment.Exit(0);                                                                            

            // }                                               






            //  MessageBox.Show("abc");
            //Thread.Sleep(2000);

            try
            {



                var request = (HttpWebRequest)WebRequest.Create("https://dj-1300382743.cos.ap-chengdu.myqcloud.com/%E5%85%AC%E5%91%8A.txt");
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd();
                textBox3.Text = responseString;

                Mp3Player mp3 = new Mp3Player();
                mp3.FileName = "download/音乐.mp3";
                mp3.play();



            }
            catch (Exception)
            {


            }



        }





        private void Form1_Load(object sender, EventArgs e)
        {


            /*
            
            var request = (HttpWebRequest)WebRequest.Create("https://dj-1300382743.cos.ap-chengdu.myqcloud.com/ver.txt");
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            */
            //if (File.Exists("game.zip"))
            //{
            //    File.Delete("game.zip");

            //}


            //string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);
            //Thread td = new Thread((ThreadStart)delegate
            //{
            //    ThreadPlayMp3();
            //});
            //td.SetApartmentState(ApartmentState.STA);
            //td.IsBackground = true;
            //td.Start();





            Thread t = new Thread((ThreadStart)delegate
            {

                printnumberswithdelay();


            });

            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            Control.CheckForIllegalCrossThreadCalls = false;
            t.Start();



            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            CreateShortcut(desktop + @"\玄鸟刀剑2.lnk", "");

            var request = (HttpWebRequest)WebRequest.Create("https://dj-1300382743.cos.ap-chengdu.myqcloud.com/url.txt");//更新game
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            request = (HttpWebRequest)WebRequest.Create(responseString);
            response = (HttpWebResponse)request.GetResponse();
            responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var responseString1 = Match_Str("分享时间:", "下载", responseString);
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


            if (responseString1 != ConfigurationManager.AppSettings["ver"])   //检查版本号是否需要升级
            {
                MessageBox.Show("程序需要更新,请点确认");
                config.AppSettings.Settings["ver"].Value = responseString1;     //更新本地版本号
                config.Save(ConfigurationSaveMode.Modified);         //保存本地版本号
                ConfigurationManager.RefreshSection("appSettings");   //刷新本地程序配置
                string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);    //匹配需要下载的地址
                System.Diagnostics.Process p = new System.Diagnostics.Process();        //声明需程序的程序
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"djupdate.exe";//需要启动的程序名                 
                p.StartInfo.Arguments = @url+" "+ AppDomain.CurrentDomain.FriendlyName; //启动参数    
                p.Start();//启动更新程序    
                          // p.WaitForExit();
                System.Environment.Exit(0);//退出当前程序
            }



















            //var request = (HttpWebRequest)WebRequest.Create("http://254595754.qzone.qq.com");


            //var response = (HttpWebResponse)request.GetResponse();
            //var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //linkLabel1.Text = responseString.ToString();





        }


        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="lnkFilePath"></param>
        /// <param name="args"></param>
        private static void CreateShortcut(string lnkFilePath, string args = "")
        {
            /// <summary>
            /// 为当前正在运行的程序创建一个快捷方式。
            /// </summary>
            /// <param name="lnkFilePath">快捷方式的完全限定路径。</param>
            /// <param name="args">快捷方式启动程序时需要使用的参数。</param>
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            var shortcut = shell.CreateShortcut(lnkFilePath);
            shortcut.TargetPath = System.Reflection.Assembly.GetEntryAssembly().Location;
            shortcut.Arguments = args;
            shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            shortcut.Save();
        }


        //播放MP3



        /// <SUMMARY>   
        /// clsMci 的摘要说明。   
        /// </SUMMARY>   
        public class Mp3Player
        {
            public Mp3Player()
            {
                //   
                // TODO: 在此处添加构造函数逻辑   
                //   
            }

            //定义API函数使用的字符串变量    
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            private string Name = "";
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            private string durLength = "";
            [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
            private string TemStr = "";
            int ilong;
            //定义播放状态枚举变量   
            public enum State
            {
                mPlaying = 1,
                mPuase = 2,
                mStop = 3
            };
            //结构变量   
            public struct structMCI
            {
                public bool bMut;
                public int iDur;
                public int iPos;
                public int iVol;
                public int iBal;
                public string iName;
                public State state;
            };

            public structMCI mc = new structMCI();

            //取得播放文件属性   
            public string FileName
            {
                get
                {
                    return mc.iName;
                }
                set
                {
                    //ASCIIEncoding asc = new ASCIIEncoding();    
                    try
                    {
                        TemStr = "";
                        TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
                        Name = Name.PadLeft(260, Convert.ToChar(" "));
                        mc.iName = value;
                        ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
                        Name = GetCurrPath(Name);
                        //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";   
                        Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
                        ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
                        ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
                        ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
                        mc.state = State.mStop;
                    }
                    catch (Exception exp)
                    {
                        // Log.Error(exp);
                        //MessageBox.Show("出错错误!");
                    }
                }
            }
            //播放   
            public void play()
            {
                TemStr = "";
                TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
                APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
                mc.state = State.mPlaying;
            }
            //停止   
            public void StopT()
            {
                TemStr = "";
                TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
                ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
                ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
                mc.state = State.mStop;
            }

            public void Puase()
            {
                TemStr = "";
                TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
                ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
                mc.state = State.mPuase;
            }
            private string GetCurrPath(string name)
            {
                if (name.Length < 1)
                    return "";
                name = name.Trim();
                name = name.Substring(0, name.Length - 1);
                return name;
            }
            //总时间   
            public int Duration
            {
                get
                {
                    durLength = "";
                    durLength = durLength.PadLeft(128, Convert.ToChar(" "));
                    APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
                    durLength = durLength.Trim();
                    if (durLength == "")
                        return 0;
                    return (int)(Convert.ToDouble(durLength) / 1000f);
                }
            }

            //当前时间   
            public int CurrentPosition
            {
                get
                {
                    durLength = "";
                    durLength = durLength.PadLeft(128, Convert.ToChar(" "));
                    APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
                    mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
                    return mc.iPos;
                }
            }
        }

        public class APIClass
        {
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern int GetShortPathName(
             string lpszLongPath,
             string shortFile,
             int cchBuffer
            );

            [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
            public static extern int mciSendString(
             string lpstrCommand,
             string lpstrReturnString,
             int uReturnLength,
             int hwndCallback
            );
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //  Form4 form4 = new Form4();
            //  form4.ShowDialog(this);

            //string a=AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName; //启动参数  

            //MessageBox.Show(a);
            //return;

            if (File.Exists("game.zip"))
            {
                File.Delete("game.zip");

            }




            //string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);

            // Thread t = new Thread(printnumberswithdelay);
            // t.Start();



            // string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            // CreateShortcut(desktop + @"\玄鸟刀剑2.lnk", "");

            var request = (HttpWebRequest)WebRequest.Create("https://dj-1300382743.cos.ap-chengdu.myqcloud.com/url.txt");//更新game
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            request = (HttpWebRequest)WebRequest.Create(responseString);
            response = (HttpWebResponse)request.GetResponse();
            responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var responseString1 = Match_Str("分享时间:", "下载", responseString);
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);         //检查版本号是否需要更新
            MessageBox.Show("程序准备强制更新,请点确认");
            config.AppSettings.Settings["ver"].Value = responseString1;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);
            
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"djupdate.exe";//需要启动的程序名  
            p.StartInfo.Arguments = @url+" "+ AppDomain.CurrentDomain.FriendlyName; //启动参数               
            p.Start();//启动更新程序    
                      // p.WaitForExit();  等侍更新程序,可不需要
            System.Environment.Exit(0);












        }

        private void Form1_Activated(object sender, EventArgs e)
        {





        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)

            {

                button2_Click(sender, e);

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("点确认将在当前目录下载刀剑客户端，约7G，文件比较大，如不是请点取消", "是否要下载客户端", MessageBoxButtons.YesNo);
            //MessageBox.Show("暂时不能用");


           // return;

            if (result == DialogResult.Yes)
            {
                var request = (HttpWebRequest)WebRequest.Create("https://cloud.189.cn/t/qE3QFvea22ye");//更新game
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

               
                var responseString1 = Match_Str("分享时间:", "下载", responseString);
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);         //检查版本号是否需要更新
                DialogResult result1=  MessageBox.Show("程序准备下载客户端,网上客户端时间"+responseString1,"请确认客户端时间",MessageBoxButtons.YesNo);

                if (result1==DialogResult.Yes)
                {
                    config.AppSettings.Settings["ver"].Value = responseString1;
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    string url = Match_Str("class=\"downloadUrl\" value=\"//", "\"", responseString);
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"djupdate.exe";//需要启动的程序名  
                    p.StartInfo.Arguments = @url; //启动参数               
                    p.Start();//启动更新程序    
                              // p.WaitForExit();  等侍更新程序,可不需要
                    System.Environment.Exit(0);

                }
                else
                {
                    return;
                }


            }
            else
            {
                return;


            }






        }
    }



}
